home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_std / native_array.e < prev    next >
Text File  |  1998-12-22  |  11KB  |  466 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. expanded class NATIVE_ARRAY[E]
  13. --
  14. -- This class gives access to the lowest level for arrays both
  15. -- for the C language and Java language.
  16. -- 
  17. -- Warning : using this class makes your Eiffel code non
  18. -- portable on other Eiffel systems.
  19. -- This class may also be modified in further release for a better
  20. -- interoperability between Java and C low level arrays.
  21. --
  22.  
  23. feature -- Basic features :
  24.  
  25.    element_sizeof: INTEGER is
  26.      -- The size in number of bytes for type `E'.
  27.       external "SmallEiffel"
  28.       end;
  29.  
  30.    calloc(nb_elements: INTEGER): like Current is
  31.      -- Allocate a new array of `nb_elements' of type `E'.
  32.      -- The new array is initialized with default values.
  33.       external "SmallEiffel"
  34.       end;
  35.  
  36.    item(index: INTEGER): E is
  37.      -- To read an `item'.
  38.      -- Assume that `calloc' is already done and that `index'
  39.      -- is the range [0 .. nb_elements-1].
  40.       external "SmallEiffel"
  41.       end;
  42.  
  43.    put(element: E; index: INTEGER) is
  44.      -- To write an item.
  45.      -- Assume that `calloc' is already done and that `index'
  46.      -- is the range [0 .. nb_elements-1].
  47.       external "SmallEiffel"
  48.       end;
  49.  
  50. feature 
  51.  
  52.    realloc(old_nb_elts, new_nb_elts: INTEGER): like Current is
  53.      -- Assume Current is a valid NATIVE_ARRAY in range 
  54.      -- [0 .. `old_nb_elts'-1]. Allocate a bigger new array in
  55.      -- range [0 .. `new_nb_elts'-1].
  56.      -- Old range is copied in the new allocated array.
  57.      -- No initialization done for new items in C.
  58.      -- Initialization is done for Java.
  59.      --
  60.       require
  61.      to_pointer.is_not_void;
  62.      old_nb_elts < new_nb_elts
  63.       do
  64.      Result := calloc(new_nb_elts);
  65.      Result.copy_from(Current,old_nb_elts - 1);
  66.       end;
  67.  
  68. feature -- Comparison :
  69.  
  70.    memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
  71.      -- True if all elements in range [0..capacity-1] are
  72.      -- identical using `equal'. Assume Current and `other' 
  73.      -- are big enough. 
  74.      -- See also `fast_memcmp'.
  75.       require
  76.      capacity > 0 implies other.to_pointer.is_not_void
  77.       local
  78.      i: INTEGER;
  79.       do
  80.      from
  81.         Result := true;
  82.         i := capacity - 1;
  83.      until
  84.         i < 0 or else not Result
  85.      loop
  86.         Result := equal_like(item(i),other.item(i));
  87.         i := i - 1;
  88.      end;
  89.       end;
  90.  
  91.    fast_memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
  92.      -- Same jobs as `memcmp' but uses infix "=" instead `equal'.
  93.       require
  94.      capacity > 0 implies other.to_pointer.is_not_void
  95.       local
  96.      i: INTEGER;
  97.       do
  98.      from
  99.         Result := true;
  100.         i := capacity - 1;
  101.      until
  102.         i < 0 or else not Result
  103.      loop
  104.         Result := item(i) = other.item(i);
  105.         i := i - 1;
  106.      end;
  107.       end;
  108.  
  109. feature -- Searching :
  110.  
  111.    index_of(element: like item; upper: INTEGER): INTEGER is
  112.      -- Give the index of the first occurrence of `element' using
  113.      -- `is_equal' for comparison.
  114.      -- Answer `upper + 1' when `element' is not inside.
  115.       require
  116.      upper >= -1
  117.       do
  118.      from  
  119.      until
  120.         Result > upper or else equal_like(element,item(Result))
  121.      loop
  122.         Result := Result + 1;
  123.      end;
  124.       end;
  125.  
  126.    fast_index_of(element: like item; upper: INTEGER): INTEGER is
  127.      -- Same as `index_of' but use basic `=' for comparison.
  128.       require
  129.      upper >= -1
  130.       do
  131.      from  
  132.      until
  133.         Result > upper or else element = item(Result)
  134.      loop
  135.         Result := Result + 1;
  136.      end;
  137.       end;
  138.  
  139. feature -- Removing :
  140.  
  141.    remove_first(upper: INTEGER) is
  142.      -- Assume `upper' is a valid index.
  143.      -- Move range [1 .. `upper'] by 1 position left.
  144.       require
  145.      upper >= 0
  146.       local
  147.      i: INTEGER;
  148.       do
  149.      from 
  150.      until
  151.         i = upper
  152.      loop
  153.         put(item(i + 1),i);
  154.         i := i + 1;
  155.      end;
  156.       end;
  157.  
  158.    remove(index, upper: INTEGER) is
  159.      -- Assume `upper' is a valid index.
  160.      -- Move range [`index' + 1 .. `upper'] by 1 position left.
  161.       require
  162.      index >= 0;
  163.      index <= upper
  164.       local
  165.      i: INTEGER;
  166.       do
  167.      from 
  168.         i := index;
  169.      until
  170.         i = upper
  171.      loop
  172.         put(item(i + 1),i);
  173.         i := i + 1;
  174.      end;
  175.       end;
  176.  
  177. feature -- Replacing :
  178.  
  179.    replace_all(old_value, new_value: like item; upper: INTEGER) is
  180.            -- Replace all occurences of the element `old_value' by `new_value' 
  181.      -- using `is_equal' for comparison.
  182.      -- See also `fast_replace_all' to choose the apropriate one.
  183.       require
  184.      upper >= -1
  185.       local
  186.      i: INTEGER;
  187.       do
  188.      from
  189.         i := upper;
  190.      until
  191.         i < 0
  192.      loop
  193.         if equal_like(old_value,item(i)) then
  194.            put(new_value,i);
  195.         end;
  196.         i := i - 1;
  197.      end;
  198.       end;
  199.  
  200.  
  201.    fast_replace_all(old_value, new_value: like item; upper: INTEGER) is
  202.            -- Replace all occurences of the element `old_value' by `new_value' 
  203.      -- using basic `=' for comparison.
  204.      -- See also `replace_all' to choose the apropriate one.
  205.       require
  206.      upper >= -1
  207.       local
  208.      i: INTEGER;
  209.       do
  210.      from
  211.         i := upper;
  212.      until
  213.         i < 0
  214.      loop
  215.         if old_value = item(i) then
  216.            put(new_value,i);
  217.         end;
  218.         i := i - 1;
  219.      end;
  220.       end;
  221.  
  222. feature -- Adding :
  223.  
  224.    copy_at(start_index: INTEGER; model: like Current; model_capacity: INTEGER) is
  225.      -- Copy range [0 .. model_capacity-1] of `model' starting to 
  226.      -- write at `start_index' of Current.
  227.      -- Range [start_index .. start_index+model_capacity] of 
  228.      -- Current is affected (assume Current is large enough).
  229.       require
  230.      start_index >= 0;
  231.      model_capacity >= 0;
  232.      model_capacity > 0 implies model.to_pointer.is_not_void
  233.       local
  234.      i1, i2: INTEGER;
  235.       do
  236.      from
  237.         i1 := start_index;
  238.      until
  239.         i2 = model_capacity
  240.      loop
  241.         put(model.item(i2),i1);
  242.         i2 := i2 + 1;
  243.         i1 := i1 + 1;
  244.      end;
  245.       end;
  246.  
  247. feature -- Other :
  248.  
  249.    set_all_with(v: like item; upper: INTEGER) is
  250.      -- Set all elements in range [0 .. upper] with
  251.      -- value `v'.
  252.       local
  253.      i: INTEGER;
  254.       do
  255.      from
  256.         i := upper;
  257.      until
  258.         i < 0
  259.      loop
  260.         put(v,i);
  261.         i := i - 1;
  262.      end;
  263.       end;
  264.  
  265.    clear_all(upper: INTEGER) is
  266.      -- Set all elements in range [0 .. `upper'] with
  267.      -- the default value.
  268.       local
  269.      v: E;
  270.      i: INTEGER;
  271.       do
  272.      from
  273.         i := upper;
  274.      until
  275.         i < 0
  276.      loop
  277.         put(v,i);
  278.         i := i - 1;
  279.      end;
  280.       end;
  281.  
  282.    clear(lower, upper: INTEGER) is
  283.      -- Set all elements in range [`lower' .. `upper'] with
  284.      -- the default value
  285.       require
  286.          lower >= 0;
  287.          upper >= lower
  288.       local
  289.          v: E;
  290.          i: INTEGER;
  291.       do
  292.          from
  293.             i := lower
  294.          until
  295.             i > upper
  296.          loop
  297.             put(v, i);
  298.             i := i + 1;
  299.          end
  300.       end
  301.  
  302.    copy_from(model: like Current; upper: INTEGER) is
  303.      -- Assume `upper' is a valid index both in Current
  304.      -- and `model'.
  305.       local
  306.      i: INTEGER;
  307.       do
  308.      from
  309.         i := upper;
  310.      until
  311.         i < 0
  312.      loop
  313.         put(model.item(i),i);
  314.         i := i - 1;
  315.      end;
  316.       end;
  317.  
  318.    move(lower, upper, offset: INTEGER) is
  319.      -- Move range [`lower' .. `upper'] by `offset' positions.
  320.      -- Freed positions are not initialized to default values.
  321.       require
  322.          lower >= 0;
  323.          upper >= lower;
  324.          lower + offset >= 0
  325.       local
  326.          i: INTEGER;
  327.       do
  328.          if offset = 0 then
  329.          elseif offset < 0 then
  330.             from
  331.                i := lower;
  332.             until
  333.                i > upper
  334.             loop
  335.                put(item(i), i + offset);
  336.                i := i + 1;
  337.             end
  338.          else
  339.             from
  340.                i := upper;
  341.             until
  342.                i < lower
  343.             loop
  344.                put(item(i), i + offset);
  345.                i := i - 1;
  346.             end
  347.          end
  348.       end
  349.  
  350.    nb_occurrences(element: like item; upper: INTEGER): INTEGER is
  351.      -- Number of occurrences of `element' in range [0..upper]
  352.      -- using `equal' for comparison.
  353.      -- See also `fast_nb_occurrences' to chose the apropriate one.
  354.       local
  355.      i: INTEGER;
  356.       do
  357.      from  
  358.         i := upper;
  359.      until
  360.         i < 0
  361.      loop
  362.         if equal_like(element,item(i)) then
  363.            Result := Result + 1;
  364.         end;
  365.         i := i - 1;
  366.      end;
  367.       end;
  368.  
  369.    fast_nb_occurrences(element: like item; upper: INTEGER): INTEGER is
  370.      -- Number of occurrences of `element' in range [0..upper]
  371.      -- using basic "=" for comparison.
  372.      -- See also `fast_nb_occurrences' to chose the apropriate one.
  373.       local
  374.      i: INTEGER;
  375.       do
  376.      from  
  377.         i := upper;
  378.      until
  379.         i < 0
  380.      loop
  381.         if element = item(i) then
  382.            Result := Result + 1;
  383.         end;
  384.         i := i - 1;
  385.      end;
  386.       end;
  387.  
  388.    all_cleared(upper: INTEGER): BOOLEAN is
  389.      -- Are all items in range [0..upper] set to default
  390.      -- values?
  391.       require
  392.      upper >= -1
  393.       local
  394.      i: INTEGER;
  395.      model: like item;
  396.       do
  397.      from
  398.         Result := true;
  399.         i := upper;
  400.      until
  401.         i < 0 or else not Result
  402.      loop
  403.         Result := model = item(i)
  404.         i := i - 1;
  405.      end;
  406.       end;
  407.  
  408. feature -- The Guru section :
  409.  
  410.    free is
  411.      -- Do nothing when producing Java byte code.
  412.      -- Do what's to be done when producing C code (depends
  413.      -- on GC is on or off).
  414.       obsolete "Replaced by automatic Garbage Collection. %
  415.              %Will be removed in the next release."
  416.       do
  417.       end;
  418.  
  419. feature -- Interfacing with C :
  420.  
  421.    to_external: POINTER is
  422.      -- Gives access to the C pointer on the area of storage.
  423.       do
  424.      Result := to_pointer;
  425.       end;
  426.  
  427.    from_pointer(pointer: POINTER): like Current is
  428.      -- Convert `pointer' into Current type.
  429.       external "SmallEiffel"
  430.       end;
  431.  
  432.    is_not_void: BOOLEAN is
  433.       do
  434.      Result := to_pointer.is_not_void;
  435.       end;
  436.  
  437. feature -- For C only :
  438.  
  439.    bytes_malloc(nb_bytes: INTEGER): like Current is
  440.       obsolete "Will be removed in the next release."
  441.       external "C_InlineWithoutCurrent"
  442.       alias "malloc"
  443.       end;
  444.  
  445. feature {NONE}
  446.  
  447.    frozen equal_like(e1, e2: like item): BOOLEAN is
  448.      -- Note: this feature is called to avoid calling `equal'
  449.      -- on expanded types (no automatic conversion to 
  450.      -- corresponding reference type).
  451.       do
  452.      if e1.is_basic_expanded_type then
  453.         Result := e1 = e2;
  454.      elseif e1.is_expanded_type then
  455.         Result := e1.is_equal(e2);
  456.      elseif e1 = e2 then
  457.         Result := true;
  458.      elseif e1 = Void or else e2 = Void then
  459.      else
  460.         Result := e1.is_equal(e2);
  461.      end;
  462.       end;
  463.  
  464. end -- NATIVE_ARRAY[E]
  465.  
  466.